route.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { getServerSideConfig } from "@/app/config/server";
  2. import {
  3. BAIDU_BASE_URL,
  4. ApiPath,
  5. ModelProvider,
  6. BAIDU_OATUH_URL,
  7. ServiceProvider,
  8. } from "@/app/constant";
  9. import { prettyObject } from "@/app/utils/format";
  10. import { NextRequest, NextResponse } from "next/server";
  11. import { auth } from "@/app/api/auth";
  12. import { isModelAvailableInServer } from "@/app/utils/model";
  13. import { getAccessToken } from "@/app/utils/baidu";
  14. const serverConfig = getServerSideConfig();
  15. async function handle(
  16. req: NextRequest,
  17. { params }: { params: { path: string[] } },
  18. ) {
  19. console.log("[Baidu Route] params ", params);
  20. if (req.method === "OPTIONS") {
  21. return NextResponse.json({ body: "OK" }, { status: 200 });
  22. }
  23. const authResult = auth(req, ModelProvider.Ernie);
  24. if (authResult.error) {
  25. return NextResponse.json(authResult, {
  26. status: 401,
  27. });
  28. }
  29. if (!serverConfig.baiduApiKey || !serverConfig.baiduSecretKey) {
  30. return NextResponse.json(
  31. {
  32. error: true,
  33. message: `missing BAIDU_API_KEY or BAIDU_SECRET_KEY in server env vars`,
  34. },
  35. {
  36. status: 401,
  37. },
  38. );
  39. }
  40. try {
  41. const response = await request(req);
  42. return response;
  43. } catch (e) {
  44. console.error("[Baidu] ", e);
  45. return NextResponse.json(prettyObject(e));
  46. }
  47. }
  48. export const GET = handle;
  49. export const POST = handle;
  50. export const runtime = "edge";
  51. export const preferredRegion = [
  52. "arn1",
  53. "bom1",
  54. "cdg1",
  55. "cle1",
  56. "cpt1",
  57. "dub1",
  58. "fra1",
  59. "gru1",
  60. "hnd1",
  61. "iad1",
  62. "icn1",
  63. "kix1",
  64. "lhr1",
  65. "pdx1",
  66. "sfo1",
  67. "sin1",
  68. "syd1",
  69. ];
  70. async function request(req: NextRequest) {
  71. const controller = new AbortController();
  72. let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Baidu, "");
  73. let baseUrl = serverConfig.baiduUrl || BAIDU_BASE_URL;
  74. if (!baseUrl.startsWith("http")) {
  75. baseUrl = `https://${baseUrl}`;
  76. }
  77. if (baseUrl.endsWith("/")) {
  78. baseUrl = baseUrl.slice(0, -1);
  79. }
  80. console.log("[Proxy] ", path);
  81. console.log("[Base Url]", baseUrl);
  82. const timeoutId = setTimeout(
  83. () => {
  84. controller.abort();
  85. },
  86. 10 * 60 * 1000,
  87. );
  88. const { access_token } = await getAccessToken(
  89. serverConfig.baiduApiKey as string,
  90. serverConfig.baiduSecretKey as string,
  91. );
  92. const fetchUrl = `${baseUrl}${path}?access_token=${access_token}`;
  93. const fetchOptions: RequestInit = {
  94. headers: {
  95. "Content-Type": "application/json",
  96. },
  97. method: req.method,
  98. body: req.body,
  99. redirect: "manual",
  100. // @ts-ignore
  101. duplex: "half",
  102. signal: controller.signal,
  103. };
  104. // #1815 try to refuse some request to some models
  105. if (serverConfig.customModels && req.body) {
  106. try {
  107. const clonedBody = await req.text();
  108. fetchOptions.body = clonedBody;
  109. const jsonBody = JSON.parse(clonedBody) as { model?: string };
  110. // not undefined and is false
  111. if (
  112. isModelAvailableInServer(
  113. serverConfig.customModels,
  114. jsonBody?.model as string,
  115. ServiceProvider.Baidu as string,
  116. )
  117. ) {
  118. return NextResponse.json(
  119. {
  120. error: true,
  121. message: `you are not allowed to use ${jsonBody?.model} model`,
  122. },
  123. {
  124. status: 403,
  125. },
  126. );
  127. }
  128. } catch (e) {
  129. console.error(`[Baidu] filter`, e);
  130. }
  131. }
  132. try {
  133. const res = await fetch(fetchUrl, fetchOptions);
  134. // to prevent browser prompt for credentials
  135. const newHeaders = new Headers(res.headers);
  136. newHeaders.delete("www-authenticate");
  137. // to disable nginx buffering
  138. newHeaders.set("X-Accel-Buffering", "no");
  139. return new Response(res.body, {
  140. status: res.status,
  141. statusText: res.statusText,
  142. headers: newHeaders,
  143. });
  144. } finally {
  145. clearTimeout(timeoutId);
  146. }
  147. }